﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.Security;
using Aj.Core;

public partial class Manager_login : PageBase
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (IsPost)
        {
            Login();
        }
    }

    private void Login()
    {
        string uname = FormParam("username");
        string pwd = Utility.Encrypt(FormParam("password"));
        string vcode = Utility.Encrypt(FormParam("verifycode").Trim().ToLower());

        if (vcode != Request.Cookies["VCode"].Value)
        {
            Response.Write("<script>alert('验证码输入有误!');</script>");
            return;
        }

        if (uname == ConfigurationManager.AppSettings["SuperuserName"])
        {
            //超级用户登陆验证
            if (pwd != ConfigurationManager.AppSettings["SuperuserPwd"])
            {
                Response.Write("<script>alert('密码错误!');</script>");
            }
            else
            {
                //超级用户登陆成功
                //1.保存验证票据
                FormsAuthentication.SetAuthCookie(uname, false);
                //2.页面跳转
                FormsAuthentication.RedirectFromLoginPage(uname, false);
            }
        }
        else
        {
            object obj = CustomQuery.SysUser_Login(uname, pwd);
            if (DBNull.Value != obj && null != obj)
            {
                //保存验证票据
                FormsAuthentication.SetAuthCookie(uname, false);
                //页面跳转
                FormsAuthentication.RedirectFromLoginPage(uname, false);
                //更新用户登录信息
                CustomQuery.SysUser_UpdateUserInfo(uname);
            }
            else
            {
                Response.Write("<script>alert('用户名或密码错误!');</script>");
            }
        }
    }
}